home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / opengl / xlib / font.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  4.5 KB  |  155 lines

  1. /*
  2.  * Copyright 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. #include <GL/glx.h>
  18. #include <GL/gl.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22.  
  23. /* prototype */
  24. void makeRasterFont(Display **dpy);
  25.  
  26. static long W = 300, H = 300;
  27. static int attributeList[] = {GLX_RGBA, None}; 
  28. static Bool WaitForNotify (Display *d, XEvent *e, char *arg)
  29. {
  30.     return (e->type == MapNotify) && (e->xmap.window == (Window) arg);
  31. }
  32.  
  33. GLuint base;
  34.  
  35. void makeRasterFont(Display **dpy)
  36. {
  37.     XFontStruct *fontInfo;
  38.     Font id;
  39.     unsigned int first, last;
  40.  
  41.     fontInfo = XLoadQueryFont(*dpy,
  42.       "-adobe-courier-bold-o-normal--20-140-100-100-m-110-iso8859-1");
  43.     if (fontInfo == NULL) {
  44.     printf("no font found\n");
  45.     exit(0);
  46.     }
  47.     id = fontInfo->fid;
  48.     first = fontInfo->min_char_or_byte2;
  49.     last = fontInfo->max_char_or_byte2;
  50.     base = glGenLists(last+1);
  51.     if (base == 0) {
  52.     printf("out of display lists\n");
  53.     exit(0);
  54.     }
  55.     glXUseXFont(id, first, last-first+1, base+first);
  56. }
  57.  
  58. void printString(char *s)
  59. {
  60.     glPushAttrib(GL_LIST_BIT);
  61.     glListBase(base);
  62.     glCallLists(strlen(s), GL_UNSIGNED_BYTE, (unsigned char *)s);
  63.     glPopAttrib();
  64. }
  65.  
  66. void gldrawing(void) {
  67.     glLoadIdentity();
  68.     glOrtho(-500.0, 500.0, -500.0, 500.0, 0.0, 1.0);
  69.     glClearColor(0.0, 0.0, 0.0, 0.0);
  70.  
  71.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  72.  
  73.     glColor3f(0.9, 0.3, 0.9);
  74.     glBegin(GL_POLYGON);
  75.     glVertex3f( 0.0, -100.0, 0.0);
  76.     glVertex3f( 0.0,  100.0, 0.0);
  77.     glVertex3f(-200.0,  100.0, 0.0);
  78.     glVertex3f(-200.0, -100.0, 0.0);
  79.     glEnd();
  80.     glColor3f(0.9, 0.9, 0.9);
  81.     glRasterPos2f(-100.0, 0.0);
  82.     printString("Here's the origin.");
  83.     glRasterPos2f(-200.0, 102.0);
  84.     printString("HERE'S THE UPPER LEFT CORNER.");
  85.  
  86.     glFlush();
  87. }
  88.  
  89. main(int argc, char **argv)
  90. {
  91.     Display *dpy;                 /* X Display */
  92.     XColor acolor;              /* Color definitions */
  93.     XVisualInfo *vi;              /* X Visual  */
  94.     Colormap cmap;                /* X Colormap */
  95.     XSetWindowAttributes swa;     /* X Window Attributes */
  96.     Window win;                   /* X Window Id */
  97.     GLXContext cx;                /* Extesion to X server; GLX context
  98.                      Data structure for X to hold GL global
  99.                      data, so that we don't have to send them
  100.                      to X through protocal everytime to
  101.                      eliminate X traffic */
  102.     XEvent event, report;         /* X event             */
  103.     int value;
  104.  
  105.                                   /* Standard code to open a X window */
  106.     dpy = XOpenDisplay (0);
  107.     XSynchronize(dpy, True);
  108.                                   /* get a appropriate visual */
  109.     vi = glXChooseVisual (dpy, DefaultScreen(dpy), attributeList);
  110.  
  111.                                       /* create a GLX context */
  112.     cx = glXCreateContext (dpy, vi, None, GL_FALSE);
  113.     
  114.                                   /* create a colormap using this visual */
  115.     cmap = XCreateColormap (dpy, RootWindow(dpy, vi->screen), vi->visual,
  116.                 AllocNone);
  117.  
  118.                                   /* create a X Window */
  119.     swa.colormap = cmap;
  120.     swa.border_pixel = 0;
  121.     swa.event_mask = StructureNotifyMask | KeyPressMask;
  122.     win = XCreateWindow (dpy, RootWindow(dpy,vi->screen), 0, 0, 500, 500,
  123.              0, vi->depth, InputOutput, vi->visual,
  124.              CWBorderPixel|CWColormap|CWEventMask, &swa);
  125.  
  126.     XSelectInput(dpy, win, ExposureMask | KeyPressMask |
  127.                                ButtonPressMask | StructureNotifyMask);
  128.     XMapWindow (dpy,win);
  129.     /* wait for window to map */
  130.     XIfEvent (dpy, &event,WaitForNotify, (char *)win);
  131.  
  132.     /* connect the context to the window */
  133.     if (!glXMakeCurrent(dpy,win,cx) == GL_TRUE) {
  134.     return 0;
  135.     }
  136.     /* make font */
  137.     makeRasterFont(&dpy);
  138.  
  139.     while (1) {
  140.     XNextEvent(dpy, &report);
  141.     switch (report.type) {
  142.         case KeyPress:
  143.         XCloseDisplay(dpy);
  144.         exit(1);
  145.         break;
  146.         case Expose:
  147.         gldrawing();
  148.         break;
  149.         default:
  150.         break;
  151.     } /*end switch*/
  152.     } /*end while*/
  153. }
  154.  
  155.